<?php

namespace common\models\business;

use Yii;
use yii\db\Query;

/**
 * This is the model class for table "business_backpig".
 *
 * @property string $id
 * @property integer $pigid
 * @property integer $farmerid
 * @property string $grossweight
 * @property string $backprice
 * @property integer $createtime
 */
class BusinessBackpig extends \yii\db\ActiveRecord
{
    //回收总价 = 回收单价+养殖成本
    public $backtotal;


    //主要用于日志记录
    public $uselog = [
        'tabcomment' => '仔猪回购单',      //用于修改了什么的显示
        'mainfield' => 'number',      //主要的显示标题的字段
        'picturefield' => [],        //图片字段
        'timefield' => [],       //时间字段
        'unsetfiled' =>[],      //不需要记录的字段
    ];





    const STATUS_DISAGREE = -1;
    const STATUS_DEFAULT = 0;
    const STATUS_AGREE = 1;

    public static $statues = [
        self::STATUS_DISAGREE => '审核不通过',
        self::STATUS_DEFAULT => '未审核',
        self::STATUS_AGREE => '审核通过',
    ];



    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'business_backpig';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['pigid','number','grossweight','backprice'], 'required'],
            [['pigid', 'farmerid'], 'integer'],
            [['number','pigno'], 'string', 'max' => 50],
            ['number','unique'],
            [['farmername','pigno'], 'string', 'max' => 20],
            [['cost'], 'string'],
            [['grossweight', 'backprice','backtotal'], 'number'],
            [['createtime'],'safe']
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'pigid' => '回购仔猪',
            'pigno' => '回购仔猪编号',
            'farmerid' => '农户',
            'grossweight' => '毛重',
            'backprice' => '回购单价',
            'createtime' => '创建时间',
            'farmername' => '农户名称',
            'cost' => '成本',
            'number' => '回购单号',
            'status' => '状态',
            'backtotal' => '回购总金额',
        ];
    }



    public function beforeSave($insert)
    {
        if($this->isNewRecord){
            $this->createtime = time();
        }
        return parent::beforeSave($insert); // TODO: Change the autogenerated stub
    }

    /**
     * @return \yii\db\ActiveQuery
     * 获取仔猪
     */
    public function getPig()
    {
        return $this->hasOne(BusinessPig::className(),['id'=>'pigid']);

    }


    /**
     * @return \yii\db\ActiveQuery
     * 回购仔猪所属农户
     */
    public function getFarmer()
    {
        return $this->hasOne(BusinessFarmers::className(),['id'=>'farmerid']);
    }





}
